/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.auth.ZAuthToken;
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import java.io.IOException;
/*     */ import junit.framework.TestCase;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.HttpException;
/*     */ import org.apache.commons.httpclient.methods.PostMethod;
/*     */ import org.apache.commons.httpclient.methods.StringRequestEntity;
/*     */ import org.junit.Assert;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestSoapHarvest
/*     */   extends TestCase
/*     */ {
/*     */   private static final String AUTH_USER_NAME = "user1";
/*     */   private static final String TARGET_USER_NAME = "user3";
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {}
/*     */   
/*     */   private String getNoOpRequest(String userId, String authToken, boolean byAccountId)
/*     */   {
/*  49 */     return "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"><soap:Header><context xmlns=\"urn:zimbra\"><userAgent name=\"Zimbra Junit\" version=\"0.0\"/>" + (authToken != null ? "<authToken>" + authToken + "</authToken>" : "") + "<nosession/>" + "<account by=\"" + (byAccountId ? "id" : "name") + "\">" + userId + "</account>" + "</context>" + "</soap:Header>" + "<soap:Body>" + "<NoOpRequest xmlns=\"urn:zimbraMail\" />" + "</soap:Body>" + "</soap:Envelope>";
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private String getInfoRequest(String userId, String authToken, boolean byAccountId)
/*     */   {
/*  67 */     return "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"><soap:Header><context xmlns=\"urn:zimbra\"><userAgent name=\"Zimbra Junit\" version=\"0.0\"/>" + (authToken != null ? "<authToken>" + authToken + "</authToken>" : "") + "<nosession/>" + "<account by=\"" + (byAccountId ? "id" : "name") + "\">" + userId + "</account>" + "</context>" + "</soap:Header>" + "<soap:Body>" + "<GetInfoRequest xmlns=\"urn:zimbraAccount\" sections=\"mbox\"/>" + "</soap:Body>" + "</soap:Envelope>";
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private String sendReq(String userId, String authToken, int expectedCode, boolean useGetInfoReq)
/*     */     throws HttpException, IOException
/*     */   {
/*  85 */     return sendReq(userId, authToken, expectedCode, useGetInfoReq, false);
/*     */   }
/*     */   
/*     */   private String sendReq(String userId, String authToken, int expectedCode, boolean useGetInfoReq, boolean byAccountId) throws HttpException, IOException {
/*  89 */     HttpClient client = new HttpClient();
/*  90 */     PostMethod method = new PostMethod(TestUtil.getSoapUrl() + (useGetInfoReq ? "GetInfoRequest" : "NoOpRequest"));
/*  91 */     method.setRequestEntity(new StringRequestEntity(useGetInfoReq ? getInfoRequest(userId, authToken, byAccountId) : getNoOpRequest(userId, authToken, byAccountId), "application/soap+xml", "UTF-8"));
/*  92 */     int respCode = HttpClientUtil.executeMethod(client, method);
/*  93 */     Assert.assertEquals(expectedCode, respCode);
/*  94 */     return method.getResponseBodyAsString();
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testHarvestNoAuth() throws Exception
/*     */   {
/* 100 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*     */     
/* 102 */     Assert.assertNotNull(mbox);
/*     */     
/* 104 */     String response = sendReq("user1", null, 500, false);
/* 105 */     Assert.assertTrue(response.indexOf("<Code>service.AUTH_REQUIRED</Code>") > -1);
/* 106 */     Assert.assertTrue(response.indexOf("<soap:Text>no valid authtoken present</soap:Text>") > -1);
/*     */     
/*     */ 
/*     */ 
/* 110 */     String bogusUserId = "bogus";
/*     */     try {
/* 112 */       TestUtil.getZMailbox(bogusUserId);
/* 113 */       Assert.fail("user " + bogusUserId + " should not exist");
/*     */     }
/*     */     catch (Exception e) {}
/*     */     
/*     */ 
/* 118 */     response = sendReq(bogusUserId, null, 500, false);
/* 119 */     Assert.assertTrue(response.indexOf("<Code>service.AUTH_REQUIRED</Code>") > -1);
/* 120 */     Assert.assertTrue(response.indexOf("<soap:Text>no valid authtoken present</soap:Text>") > -1);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testHarvestDelegated()
/*     */     throws Exception
/*     */   {
/* 127 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*     */     
/* 129 */     Assert.assertNotNull(mbox);
/*     */     
/* 131 */     String authToken = mbox.getAuthToken().getValue();
/* 132 */     String response = sendReq("user1", authToken, 200, true);
/*     */     
/*     */ 
/* 135 */     String userId = "user3";
/* 136 */     mbox = TestUtil.getZMailbox(userId);
/*     */     
/* 138 */     Assert.assertNotNull(mbox);
/*     */     
/*     */ 
/* 141 */     response = sendReq(userId, authToken, 500, true);
/* 142 */     Assert.assertTrue(response.indexOf("<Code>service.PERM_DENIED</Code>") > -1);
/* 143 */     Assert.assertTrue(response.indexOf("<soap:Text>permission denied: can not access account") > -1);
/*     */     
/* 145 */     Assert.assertTrue(!response.contains(Provisioning.getInstance().get(Key.AccountBy.name, userId).getId()));
/*     */     
/* 147 */     response = sendReq(userId, authToken, 500, false);
/* 148 */     Assert.assertTrue(response.indexOf("<Code>service.PERM_DENIED</Code>") > -1);
/* 149 */     Assert.assertTrue(response.indexOf("<soap:Text>permission denied: can not access account") > -1);
/*     */     
/* 151 */     Assert.assertTrue(!response.contains(Provisioning.getInstance().get(Key.AccountBy.name, userId).getId()));
/*     */     
/*     */ 
/* 154 */     userId = "bogus";
/*     */     try {
/* 156 */       TestUtil.getZMailbox(userId);
/* 157 */       Assert.fail("user " + userId + " should not exist");
/*     */     }
/*     */     catch (Exception e) {}
/*     */     
/*     */ 
/* 162 */     response = sendReq(userId, authToken, 500, true);
/* 163 */     Assert.assertTrue(response.indexOf("<Code>service.PERM_DENIED</Code>") > -1);
/* 164 */     Assert.assertTrue(response.indexOf("<soap:Text>permission denied: can not access account") > -1);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testErrorResponses() throws Exception {
/* 169 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 170 */     String authToken = mbox.getAuthToken().getValue();
/* 171 */     String userId = "user3";
/* 172 */     Account account = Provisioning.getInstance().get(Key.AccountBy.name, userId);
/*     */     
/*     */ 
/* 175 */     String response = sendReq(userId, authToken, 500, true, false);
/* 176 */     Assert.assertTrue(response.indexOf("<Code>service.PERM_DENIED</Code>") > -1);
/* 177 */     Assert.assertTrue(response.indexOf("<soap:Text>permission denied: can not access account") > -1);
/*     */     
/* 179 */     Assert.assertTrue(!response.contains(account.getId()));
/*     */     
/* 181 */     response = sendReq(account.getId(), authToken, 500, true, true);
/* 182 */     Assert.assertTrue(response.indexOf("<Code>service.PERM_DENIED</Code>") > -1);
/* 183 */     Assert.assertTrue(response.indexOf("<soap:Text>permission denied: can not access account") > -1);
/*     */     
/* 185 */     Assert.assertTrue(!response.contains(userId));
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testHarvestDelegatedNoOp()
/*     */     throws Exception
/*     */   {
/* 192 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*     */     
/* 194 */     Assert.assertNotNull(mbox);
/*     */     
/* 196 */     String authToken = mbox.getAuthToken().getValue();
/* 197 */     String response = sendReq("user1", authToken, 200, false);
/*     */     
/*     */ 
/* 200 */     String userId = "user3";
/* 201 */     mbox = TestUtil.getZMailbox(userId);
/*     */     
/* 203 */     Assert.assertNotNull(mbox);
/*     */     
/*     */ 
/* 206 */     response = sendReq(userId, authToken, 500, true);
/* 207 */     Assert.assertTrue(response.indexOf("<Code>service.PERM_DENIED</Code>") > -1);
/* 208 */     Assert.assertTrue(response.indexOf("<soap:Text>permission denied: can not access account") > -1);
/*     */     
/* 210 */     Assert.assertTrue(!response.contains(Provisioning.getInstance().get(Key.AccountBy.name, userId).getId()));
/*     */     
/* 212 */     response = sendReq(userId, authToken, 500, false);
/* 213 */     Assert.assertTrue(response.indexOf("<Code>service.PERM_DENIED</Code>") > -1);
/* 214 */     Assert.assertTrue(response.indexOf("<soap:Text>permission denied: can not access account") > -1);
/*     */     
/* 216 */     Assert.assertTrue(!response.contains(Provisioning.getInstance().get(Key.AccountBy.name, userId).getId()));
/*     */     
/*     */ 
/* 219 */     userId = "bogus";
/*     */     try {
/* 221 */       TestUtil.getZMailbox(userId);
/* 222 */       Assert.fail("user " + userId + " should not exist");
/*     */     }
/*     */     catch (Exception e) {}
/*     */     
/*     */ 
/* 227 */     response = sendReq(userId, authToken, 500, false);
/* 228 */     Assert.assertTrue(response.indexOf("<Code>service.PERM_DENIED</Code>") > -1);
/* 229 */     Assert.assertTrue(response.indexOf("<soap:Text>permission denied: can not access account") > -1);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testAdminDelegation() throws Exception
/*     */   {
/* 235 */     ZMailbox mbox = TestUtil.getZMailbox("admin");
/*     */     
/* 237 */     String authToken = mbox.getAuthToken().getValue();
/* 238 */     String response = sendReq("admin", authToken, 200, false);
/*     */     
/* 240 */     String userId = "user3";
/* 241 */     mbox = TestUtil.getZMailbox(userId);
/*     */     
/* 243 */     Assert.assertNotNull(mbox);
/*     */     
/*     */ 
/* 246 */     response = sendReq(userId, authToken, 500, true);
/* 247 */     Assert.assertTrue(response.indexOf("<Code>service.PERM_DENIED</Code>") > -1);
/* 248 */     Assert.assertTrue(response.indexOf("<soap:Text>permission denied: can not access account") > -1);
/*     */     
/* 250 */     Assert.assertTrue(!response.contains(Provisioning.getInstance().get(Key.AccountBy.name, userId).getId()));
/*     */     
/* 252 */     response = sendReq(userId, authToken, 500, false);
/* 253 */     Assert.assertTrue(response.indexOf("<Code>service.PERM_DENIED</Code>") > -1);
/* 254 */     Assert.assertTrue(response.indexOf("<soap:Text>permission denied: can not access account") > -1);
/*     */     
/* 256 */     Assert.assertTrue(!response.contains(Provisioning.getInstance().get(Key.AccountBy.name, userId).getId()));
/*     */     
/*     */ 
/* 259 */     userId = "bogus";
/*     */     try {
/* 261 */       TestUtil.getZMailbox(userId);
/* 262 */       Assert.fail("user " + userId + " should not exist");
/*     */     }
/*     */     catch (Exception e) {}
/*     */     
/*     */ 
/* 267 */     response = sendReq(userId, authToken, 500, false);
/* 268 */     Assert.assertTrue(response.indexOf("<Code>service.PERM_DENIED</Code>") > -1);
/* 269 */     Assert.assertTrue(response.indexOf("<soap:Text>permission denied: can not access account") > -1);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testAdminAuthToken() throws Exception
/*     */   {
/* 275 */     ZMailbox mbox = TestUtil.getZMailboxAsAdmin("admin");
/*     */     
/* 277 */     String authToken = mbox.getAuthToken().getValue();
/* 278 */     String response = sendReq("admin", authToken, 200, false);
/*     */     
/*     */ 
/* 281 */     String userId = "user3";
/* 282 */     mbox = TestUtil.getZMailbox(userId);
/*     */     
/* 284 */     Assert.assertNotNull(mbox);
/*     */     
/*     */ 
/* 287 */     response = sendReq(userId, authToken, 200, false);
/* 288 */     response = sendReq(userId, authToken, 200, true);
/*     */     
/*     */ 
/* 291 */     userId = "bogus";
/*     */     try {
/* 293 */       TestUtil.getZMailbox(userId);
/* 294 */       Assert.fail("user " + userId + " should not exist");
/*     */     }
/*     */     catch (Exception e) {}
/*     */     
/*     */ 
/* 299 */     response = sendReq(userId, authToken, 500, false);
/*     */     
/* 301 */     Assert.assertTrue(response.indexOf("<Code>account.NO_SUCH_ACCOUNT</Code>") > -1);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestSoapHarvest.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */